// FindEye.cpp: implementation of the CFindEye_HL class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IrIdentify.h"
#include "FindEye_HL.h"
#include "math.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void CFindEye_HL::SAFE_RELEASE300(unsigned char** ppImg, int nDim )
{
	if(ppImg)
	{
		for(int i = 0; i < nDim; i++)
			SAFE_RELEASE(ppImg[i]);
		SAFE_RELEASE(ppImg);
	}
}

void CFindEye_HL::SAFE_RELEASE30(int** ppImg, int nDim )
{
	if(ppImg)
	{
		for(int i = 0; i < nDim; i++)
			SAFE_RELEASE(ppImg[i]);
		SAFE_RELEASE(ppImg);
	}
}

CFindEye_HL::CFindEye_HL()
{
   m_nStep=1;
   m_fRatio=1.0f;
   SubWindowWidth=40;
   SubWindowHeight=40;
   m_nMaxFeatureNum = 8000;
   NULL_POINTER(m_nModel);
   NULL_POINTER(m_pModel);
   NULL_POINTER(m_ppGrayImg);
   NULL_POINTER(m_ppItgImg);
//   NULL_POINTER(ItgImage);
   for( int i=0; i< m_nModel ;i++)
		m_pArrModel[i]= NULL;
}

CFindEye_HL::~CFindEye_HL()
{
//   SAFE_RELEASE300(m_ppGrayImg,  m_nWidth);
 //  SAFE_RELEASE30(m_ppItgImg,  m_nWidth+1);
//   SAFE_RELEASE3(ItgImage,  41);
   SAFE_RELEASE_EYE_MODEL(m_pModel);
   for(int i = 0; i < m_nModel; i++)
	  SAFE_RELEASE_EYE_MODEL(m_pArrModel[i]);
}

/*double CFindEye_HL::GetFilterValueWithImage(BYTE **ppImageData, int FilterNo, int StartX, int StartY)
{
    double CenterSum=0.0f;
	int RingSum=0;
	double ss;
    int k,j;
	for(k=StartX;k<StartX+40;k++)
	      for(j=StartY;j<StartY+40;j++)
		  {
			CenterSum+=Center[k][j]& ppImageData[k][j]; 
            RingSum+=Ring[FilterNo][k][j]& ppImageData[k][j];
		  }
    if(FilterNo!=(TOTALFILTERNUM-1))
	{
		if(RingSum==0)
			RingSum=1;
		ss=double(CenterSum/RingSum);
	}
	else
		ss=CenterSum;
	return ss;
}*/
void CFindEye_HL::SAFE_RELEASE_EYE_MODEL(EyeModel_HL *pModel)
{
   if(pModel != NULL)
	{
		SAFE_RELEASE(pModel->finalthreshold);				
		SAFE_RELEASE(pModel->cascade);
		SAFE_RELEASE(pModel->finalh);
		SAFE_RELEASE(pModel->finalsign);
		SAFE_RELEASE(pModel->errorset);
		SAFE_RELEASE(pModel->featureset);	
		SAFE_RELEASE(pModel->apha);

		// potential memory leak...
		delete []pModel->finaltype;
		delete []pModel->featurescale;
		pModel->finaltype    = NULL;
		pModel->featurescale = NULL; 

		delete pModel;
		pModel = NULL;
	}
}

bool CFindEye_HL::DcEyes(int PosX,int PosY,double* mScore)
{
    int layer, t, tTh;
	double hvalue,hTh;
	double comb_Conf = 0;


/*    CFile curFile;
	curFile.Open("C:\\test.DB", CFile::modeCreate|CFile::modeWrite);
	

		for( i = 0; i < 40+1; i++)
			curFile.Write(ppffItgImage[i], sizeof(int)*(40+1));
	
	curFile.Close();*/
 //  SaveBmpImg("C:\\test.bmp",TmpImage,40,40);


//////////////////Attention//////////////////
	int CenterSum=0;
	CenterSum=m_ppItgImg[19+PosX+2][19+PosY+2]+m_ppItgImg[19+PosX][19+PosY]-m_ppItgImg[19+PosX+2][19+PosY]-m_ppItgImg[19+PosX][19+PosY+2];
    if(CenterSum<100)
  		return false;

	for(int i =0 ; i< m_nModel; i++)
	{
	
       if(m_pArrModel[i]==NULL)
	   {
		 continue;
	   }
			// tested the orginal images
	   layer=1; 
	   hvalue=0;	
       do
	   {
			t=m_pArrModel[i]->cascade[layer-1];
			tTh=m_pArrModel[i]->cascade[layer];
			hTh=m_pArrModel[i]->finalthreshold[layer];
				
			while ((t<tTh))
			{

				if((m_pArrModel[i]->finalh[t]> 
					GetFilterValueWithImage(m_ppItgImg, 
									m_pArrModel[i]->featureset[t],
									m_pArrModel[i]->finaltype[t][0],
				                    m_pArrModel[i]->finaltype[t][1],
									m_pArrModel[i]->finaltype[t][2],
                                            PosX,
											PosY)) != (m_pArrModel[i]->finalsign[t]>0))
							
							hvalue+=m_pArrModel[i]->apha[t];
							
						t++;
			}	
			 layer++;
	   
	   }while((hvalue>=m_pArrModel[i]->finalthreshold[layer-1]) && (layer <= m_pArrModel[i]->m_nMaxLayNum));
		if(hvalue<m_pArrModel[i]->finalthreshold[layer-1]) 
		{
		
			return false;
		}
		comb_Conf += hvalue/m_pArrModel[i]->finalthreshold[layer-1];
	}
	*mScore=hvalue;

	if(comb_Conf>=m_nModel*m_fRatio)
			return true;	
	return false;
}

BOOL CFindEye_HL::LoadModelFromFile()
{
	if(0)
	{
		CString szModelFileName1[10];

		FILE *fp;
		char temp[100];

	//load current path module
		CString strModuleFile;
		CString strIniFile;
		char pModuleName[256];
		::GetModuleFileName(g_hModule,pModuleName,256);
		CString strTemp;
		strTemp=pModuleName;
		int pos=strTemp.ReverseFind('\\');
		strModuleFile=strTemp.Left(pos);
		strIniFile=strModuleFile+"\\EyeModel_HL.ini";

		fp = fopen((LPCTSTR)strIniFile, "r");

		if(!fp) return FALSE;

	//	AfxMessageBox(strIniFile);
		
		fscanf(fp, "%d",&m_nModel);

	//	int k;
		for(int i = 0; i < m_nModel ; i++)
		{
			fscanf(fp, "%s ", temp);
			szModelFileName1[i] = temp;
	//		CString str=szModelFileName1[i];
	//		fscanf(fp, "%d", &k);
			BOOL b = LoadOneModelFromFile(&m_pArrModel[i], strModuleFile+"\\"+szModelFileName1[i]); 
			if(!b)
			{

				AfxMessageBox(strModuleFile+"\\"+szModelFileName1[i]);
				return b;
			}
	//		m_fThr[i] = m_pArrModel[i]->finalthreshold[m_pArrModel[i]->m_nMaxLayNum];
		}
	}
	else
	{
		m_nModel=1;
		for(int i=0;i<m_nModel;i++)
		{
			BOOL b=LoadOneModelFromResource(&m_pArrModel[i]);
			if(!b)
			{
				return b;
			}
		}

	}


//	AfxMessageBox("end");
	return TRUE;
}

BOOL CFindEye_HL::LoadOneModelFromFile(EyeModel_HL **m_ppModel, CString szModelFileName)
{
    SAFE_RELEASE_EYE_MODEL(m_pModel);

	EyeModel_HL* m_pModel = new EyeModel_HL;
	ASSERT(m_pModel);


	int m_nMaxLayerNum = 9000;
	int m_nMaxFilterNum = 10000;

	m_pModel->m_nMaxLayNum = m_nMaxLayerNum;
	m_pModel->maxT         = m_nMaxFilterNum;

	// Allocate memory...

	if(NULL==(m_pModel->cascade=new int[m_nMaxLayerNum+1]))
		return FALSE;
	if(NULL==(m_pModel->finalthreshold=new double[m_nMaxLayerNum+1]))
		return FALSE;
	if(NULL==(m_pModel->finaltype=new int[m_nMaxFilterNum][3]))
		return FALSE;
	if(NULL==(m_pModel->finalh=new double[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->finalsign=new int[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->errorset=new double[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->featureset=new int[m_nMaxFilterNum]))
		return FALSE;
//	if(NULL==(m_pModel->featurescale=new int[m_nMaxFilterNum][4]))
//		return FALSE;
	if(NULL==(m_pModel->apha=new double[m_nMaxFilterNum]))
		return FALSE;
	

	FILE *fpNewModel;
	if( NULL == (fpNewModel = fopen(szModelFileName, "rb")))
		return FALSE;

	fread(&m_pModel->cascade[0],        sizeof(int),    1, fpNewModel);
	fread(&m_pModel->finalthreshold[0], sizeof(double), 1, fpNewModel);
//	TrshHold[0] = m_pModel->finalthreshold[0];

	int nLayerNum = 0;
	int count = 0;
	int nByte = 1;

	while(nByte!=0)
	{
		nLayerNum++;
		
		nByte = fread(&m_pModel->cascade[nLayerNum],        sizeof(int),    1, fpNewModel);
		if(nByte <= 0 || nLayerNum == m_nMaxLayerNum+1)
			break;

		nByte = fread(&m_pModel->finalthreshold[nLayerNum], sizeof(double), 1, fpNewModel);
//		TrshHold[nLayerNum] = m_pModel->finalthreshold[nLayerNum];

		int nWLNum = m_pModel->cascade[nLayerNum]-m_pModel->cascade[nLayerNum-1];
		m_nFilterNum  = m_pModel->cascade[nLayerNum];
		for(int i = 0; i < nWLNum; i++)
		{
			
			nByte = fread(&m_pModel->finalh[count],          sizeof(double), 1, fpNewModel); 
			nByte = fread(&m_pModel->finalsign[count],       sizeof(int),    1, fpNewModel);
			nByte = fread(&m_pModel->errorset[count],        sizeof(double), 1, fpNewModel);
			nByte = fread(&m_pModel->featureset[count],      sizeof(int),    1, fpNewModel);
			nByte = fread(m_pModel->finaltype[count],        sizeof(int),    3, fpNewModel);

	    	count++;
		}
	}



	CFindEye_HL::m_nMaxLayerNum = nLayerNum - 1;
	m_pModel->m_nMaxLayNum = nLayerNum - 1;
	m_pModel->maxT = count;
	m_nMaxFilterNum=m_pModel->cascade[nLayerNum-1];
	for (int t=0;t<m_nMaxFilterNum; t++)
		m_pModel->apha[t]=log((1-m_pModel->errorset[t])/m_pModel->errorset[t]);
	fclose(fpNewModel);

	if(0)
	{
	CString str;
	str.Format("%d,%d,%d",nLayerNum,count,m_nMaxFilterNum);
	AfxMessageBox(str);
	}
	
	*m_ppModel = m_pModel;

    return true;
/*	float hvalue=0;							
	int tTh, hTh, layer=1;
	do
	{
		t=m_pModel->cascade[layer-1];
		tTh=m_pModel->cascade[layer];
		hTh=m_pModel->finalthreshold[layer];
		
		while ((t<tTh))
		{
			hvalue+=m_pModel->apha[t];
			t++;
		}
		m_fConfThr[layer-1] = hvalue;
		layer = layer +1;
	}while(layer <= CFaceFinder::m_nMaxLayerNum);*/

//	m_CurModelPath = szModelFileName;
//	*m_ppModel = m_pModel;
}
BOOL CFindEye_HL::LoadOneModelFromResource(EyeModel_HL **m_ppModel)
{
    SAFE_RELEASE_EYE_MODEL(m_pModel);

	EyeModel_HL* m_pModel = new EyeModel_HL;
	ASSERT(m_pModel);


	int m_nMaxLayerNum = 9000;
	int m_nMaxFilterNum = 10000;

	m_pModel->m_nMaxLayNum = m_nMaxLayerNum;
	m_pModel->maxT         = m_nMaxFilterNum;

	// Allocate memory...
	if(NULL==(m_pModel->cascade=new int[m_nMaxLayerNum+1]))
		return FALSE;
	if(NULL==(m_pModel->finalthreshold=new double[m_nMaxLayerNum+1]))
		return FALSE;
	if(NULL==(m_pModel->finaltype=new int[m_nMaxFilterNum][3]))
		return FALSE;
	if(NULL==(m_pModel->finalh=new double[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->finalsign=new int[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->errorset=new double[m_nMaxFilterNum]))
		return FALSE;
	if(NULL==(m_pModel->featureset=new int[m_nMaxFilterNum]))
		return FALSE;
//	if(NULL==(m_pModel->featurescale=new int[m_nMaxFilterNum][4]))
//		return FALSE;
	if(NULL==(m_pModel->apha=new double[m_nMaxFilterNum]))
		return FALSE;
	
	// reading model......
	char pModuleName[256];
	::GetModuleFileName(g_hModule,pModuleName,256);
    LPVOID pv_Feature=LoadDataResource("IDR_EYE_MODEL_HL",pModuleName);

	if(pv_Feature)
	{
	//	AfxMessageBox(pModuleName);
	}
	else
	{
		AfxMessageBox("Load EyeModal error!");
		return FALSE;
	}

	const char *fpNewModel = (const char *)pv_Feature;

	const char *pTemp = fpNewModel;
//	sscanf(&m_pModel->cascade[0],        sizeof(int),    1, fpNewModel);
	const int *p_int = (int *) pTemp;
	m_pModel->cascade[0] = *p_int;
	pTemp += sizeof(int);

//	fread(&m_pModel->finalthreshold[0], sizeof(double), 1, fpNewModel);
	const double *p_double = (double *) pTemp;
	m_pModel->finalthreshold[0] = *p_double;
	pTemp += sizeof(double);

//	TrshHold[0] = m_pModel->finalthreshold[0];

	int nLayerNum = 0;
	int count = 0;
	int nByte = 1;

//	char* pv_LayerNum=(char*)LoadDataResource("IDR_EYE_MODEL_LAYER",pModuleName);
	int nLayer=8;
//	sscanf(pv_LayerNum,"%d",&nLayer);
	
	for(int ii=1;ii<=nLayer;ii++)//9 layer
	{
		nLayerNum++;
		
//		nByte = fread(&m_pModel->cascade[nLayerNum],        sizeof(int),    1, fpNewModel);
		p_int = (int *) pTemp;
		m_pModel->cascade[nLayerNum] = *p_int;
		pTemp += sizeof(int);	
		nByte = sizeof(int);

		if(nByte <= 0 || nLayerNum == m_nMaxLayerNum+1)
			break;

//		nByte = fread(&m_pModel->finalthreshold[nLayerNum], sizeof(double), 1, fpNewModel);
		p_double = (double *) pTemp;
		m_pModel->finalthreshold[nLayerNum] = *p_double;
		pTemp += sizeof(double);

//		TrshHold[nLayerNum] = m_pModel->finalthreshold[nLayerNum];

		int nWLNum = m_pModel->cascade[nLayerNum]-m_pModel->cascade[nLayerNum-1];
		m_nFilterNum  = m_pModel->cascade[nLayerNum];
		for(int i = 0; i < nWLNum; i++)
		{
//			nByte = fread(&m_pModel->finalh[count],          sizeof(double), 1, fpNewModel); 
			p_double = (double *) pTemp;
			m_pModel->finalh[count] = *p_double;
			pTemp += sizeof(double);
			
//			nByte = fread(&m_pModel->finalsign[count],       sizeof(int),    1, fpNewModel);
			p_int = (int *) pTemp;
			m_pModel->finalsign[count] = *p_int;
			pTemp += sizeof(int);
			
//			nByte = fread(&m_pModel->errorset[count],        sizeof(double), 1, fpNewModel);
			p_double = (double *) pTemp;
			m_pModel->errorset[count] = *p_double;
			pTemp += sizeof(double);

//			nByte = fread(&m_pModel->featureset[count],      sizeof(int),    1, fpNewModel);
			p_int = (int *) pTemp;
			m_pModel->featureset[count] = *p_int;
			pTemp += sizeof(int);
						
//			nByte = fread(m_pModel->finaltype[count],        sizeof(int),    3, fpNewModel);
			p_int = (int *) pTemp;
			m_pModel->finaltype[count][0] = *p_int;
			pTemp += sizeof(int);

			p_int = (int *) pTemp;
			m_pModel->finaltype[count][1] = *p_int;
			pTemp += sizeof(int);

			p_int = (int *) pTemp;
			m_pModel->finaltype[count][2] = *p_int;
			pTemp += sizeof(int);

	    	count++;
		}
	}

	nLayerNum++;
	//////////////////
	CFindEye_HL::m_nMaxLayerNum = nLayerNum - 1;
	m_pModel->m_nMaxLayNum = nLayerNum - 1;
	m_pModel->maxT = count;
	m_nMaxFilterNum=m_pModel->cascade[nLayerNum-1];
	for (int t=0;t<m_nMaxFilterNum; t++)
		m_pModel->apha[t]=log((1-m_pModel->errorset[t])/m_pModel->errorset[t]);
	*m_ppModel = m_pModel;

	if(0)
	{
	CString str;
	str.Format("%d,%d,%d",nLayerNum,count,m_nMaxFilterNum);
	AfxMessageBox(str);
	}

    return TRUE;

}
////////////
#define Top_Ratio 0.15
#define Bottom_Ratio 0.35
#define Left_Ratio_Leye 0.16
#define Right_Ratio_Leye 0.37
#define Right_Ratio_Reye 0.17
#define Left_Ratio_Reye 0.35
#define MicroSearchRange

void CFindEye_HL::GetEyePix(BYTE **pOriImage, int Width, int Height,RECT DetectedFace,POINT* templ,POINT* tempr)
{
    m_ppGrayImg=pOriImage;
	m_nWidth=Width;
	m_nHeight=Height;
    
    m_ppItgImg=new int*[m_nWidth+1];
	for(int i=0;i<m_nWidth+1;i++)
		m_ppItgImg[i]=new int[m_nHeight+1];

    
	GetItgImage(m_ppItgImg,m_ppGrayImg,m_nWidth,m_nHeight);
   
 //   SaveBmpImg("C:\\test.bmp",tempGrayImg,m_nWidth,m_nHeight);
    
    int px, py;
//	int DcEyeNum=0;
	px=DetectedFace.left+20;
	int cropWidth=(DetectedFace.left+DetectedFace.right)/2;
	int maxl=0;int maxr=0;

	
    double lmax=0.0f;
	double rmax=0.0f;
    double *tmpscore=new double;
    *tmpscore=0;
	int nCount=0;
 /*   int nCount1=0;
	int nCount2=0;*/
	//////////////////////debug for zl/////////////////////
    templ->x=0;
	templ->y=0;
	tempr->x=0;
	tempr->y=0;
	m_nStep=1;
#ifdef MicroSearchRange
	int SearchTop=0,SearchBottom=0,Searchleft_leye=0,Searchright_leye=0,Searchleft_reye=0,Searchright_reye=0;
	int FaceWindowWidth=DetectedFace.bottom-DetectedFace.top;
    SearchTop=FaceWindowWidth*Top_Ratio;
    SearchBottom=FaceWindowWidth*Bottom_Ratio;
    Searchleft_leye=FaceWindowWidth*Left_Ratio_Leye;
    Searchright_leye=FaceWindowWidth*Right_Ratio_Leye;
    Searchleft_reye=FaceWindowWidth*(1-Left_Ratio_Reye);
    Searchright_reye=FaceWindowWidth*(1-Right_Ratio_Reye);
	px=DetectedFace.left+Searchleft_leye-19;

	while((px+19<DetectedFace.left+Searchright_leye)||((px+19>=DetectedFace.left+Searchleft_reye)&&(px+19<DetectedFace.left+Searchright_reye)))
	{
		py=DetectedFace.top+SearchTop-19;
		while((py+19)<DetectedFace.top+SearchBottom)
		{
#else	
    while((px+SubWindowWidth<DetectedFace.right-20))
	{
	   py=DetectedFace.top+5;
	   while((py+SubWindowHeight<(DetectedFace.bottom+DetectedFace.top)/2-10))
	   {
#endif
		   BOOL bRsl;
           bRsl = DcEyes(px, py,tmpscore);
		   if( bRsl )		//		if(GetFaceConf(px, py, winsize, dbConfidence, fMultiConf))
		   {
			 /*  if(px<cropWidth-5)
			   {if(nCount1<2000)
			   {templ[nCount1]->x=px;
               templ[nCount1]->y=py;
			   nCount1++;
			   }}
			   if(px>cropWidth+5)
			   {if(nCount2<2000)
			   {tempr[nCount2]->x=px;
               tempr[nCount2]->y=py;
			   nCount2++;
			   }}*/
			//////////////////////debug for zl/////////////////////
			   nCount++;
               if(px<cropWidth-5)
			   {
			//	  if(m_ppGrayImg[px+19][py+19]>maxl)
				  if((*tmpscore)>lmax)
				  {
					 maxl=m_ppGrayImg[px+19][py+19];
					 (*templ).x=px+19;
					 (*templ).y=(py+19);
	                 lmax=*tmpscore;
				  }
			   }
			   if(px>cropWidth+5)
			   {
            //      if(m_ppGrayImg[px+19][py+19]>maxr)
				  if((*tmpscore)>rmax)
				  {
					 maxr=m_ppGrayImg[px+19][py+19];
					 (*tempr).x=px+19;
					 (*tempr).y=(py+19);
                      rmax=*tmpscore;
				  }
			   }
			   
			//////////////////////debug for zl/////////////////////
			 
		   }
		   py+=m_nStep;
	   }
	   px+=m_nStep; 
#ifdef MicroSearchRange
	   if(px+19==DetectedFace.left+Searchright_leye)
		   px=DetectedFace.left+Searchleft_reye-19;
#endif
	}

	delete tmpscore;

	for(int i=0;i<m_nWidth+1;i++)
	{
		delete []m_ppItgImg[i];
	}
	delete []m_ppItgImg;
	m_ppItgImg=NULL;
	m_ppGrayImg=NULL;

/*	*maxl=nCount1;
	*maxr=nCount2;*/
//	SAFE_RELEASE30(m_ppItgImg,  m_nWidth+1);
	
}


void CFindEye_HL::SaveBmpImg(CString szFileName, BYTE **ppImg, int nWidth, int nHeight)
{
    BYTE* pImg = new BYTE[nWidth*nHeight];
	float dbMin = 999999.9f;
	float dbMax = -999999.9f;

	for(int i = 0; i < nWidth; i++)
	{
		for(int j = 0; j < nHeight; j++)
		{
			if(dbMin > ppImg[i][j])
				dbMin = ppImg[i][j];
			
			if(dbMax < ppImg[i][j])
				dbMax = ppImg[i][j];
		}
	}
	
	for(int i = 0; i < nHeight; i++)
	{
		for(int j = 0; j < nWidth; j++)
		{
			pImg[(nHeight-1-i)*nWidth+j] = (ppImg[j][i]-dbMin)*255/(dbMax-dbMin);
		}
	}

	BITMAPFILEHEADER bmfHdr;

	int nImgHeight=(nWidth+3)/4*4;
	int imageSize=nImgHeight*nHeight;
	
	BYTE *saveImage=new BYTE[imageSize];
	memset(saveImage,0,imageSize);
	for(int i=0;i<nHeight;i++)
		memcpy(saveImage+i*nImgHeight,pImg+i*nWidth,nWidth);

	bmfHdr.bfType=0x4d42; 
    bmfHdr.bfSize=imageSize+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD); 
    bmfHdr.bfReserved1=0; 
    bmfHdr.bfReserved2=0; 
    bmfHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD); 

	BITMAPINFO *pInfo=(BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));
	pInfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
    pInfo->bmiHeader.biWidth=nWidth; 
    pInfo->bmiHeader.biHeight=nHeight; 
    pInfo->bmiHeader.biPlanes=1; 
    pInfo->bmiHeader.biBitCount=8; 
    pInfo->bmiHeader.biCompression=BI_RGB; 
    pInfo->bmiHeader.biSizeImage=imageSize; 
    pInfo->bmiHeader.biXPelsPerMeter=0; 
    pInfo->bmiHeader.biYPelsPerMeter=0; 
    pInfo->bmiHeader.biClrUsed=0; 
    pInfo->bmiHeader.biClrImportant=0; 

  	for(int i=0;i<256;i++)
	{
		pInfo->bmiColors[i].rgbBlue=i;
		pInfo->bmiColors[i].rgbGreen=i;
		pInfo->bmiColors[i].rgbRed=i;
		pInfo->bmiColors[i].rgbReserved=0;
	}

	CFile mFile;
	mFile.Open(szFileName,CFile::modeCreate | CFile::modeWrite,NULL);
    mFile.Write((LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER));
    mFile.Write(pInfo,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));
	mFile.Write(saveImage,(DWORD)imageSize); 
	mFile.Close();
	
	free(pInfo);

	SAFE_RELEASE(pImg);
}


double CFindEye_HL::GetFilterValueWithImage(int **ppzlImageData, int FilterNo, int RECTWidth,int Posx,int Posy,int StartX, int StartY)
{
//	FILE *fp;
//	fp=fopen("C:\\ForTest\\Hist.txt","a+t");
	double CenterSum=0.0f;
	int HaarSum=0;
	double ss;
 /*   int k,j;
	int i,s;
	for(k=StartX;k<StartX+40;k++)
	{
	    for(j=StartY;j<StartY+40;j++)
		{
			CenterSum+=Center[k-StartX][j-StartY]& ppImageData[k][j]; 
 //           RingSum+=Ring[FilterNo][k-StartX][j-StartY]& ppImageData[k][j];
		}
	}*/


	CenterSum=ppzlImageData[19+StartX+2][19+StartY+2]+ppzlImageData[19+StartX][19+StartY]-ppzlImageData[19+StartX+2][19+StartY]-ppzlImageData[19+StartX][19+StartY+2];
	HaarSum=GetFeatureValue(ppzlImageData,RECTWidth,Posx+StartX,Posy+StartY);

		if(CenterSum==0)
			CenterSum=1;
		ss=double(HaarSum/CenterSum);

//	if(FilterNo==2)
//	fprintf(fp,"%f\n",ss);
//	fclose(fp);
	return ss;
	
}


int CFindEye_HL::GetFeatureValue(int **ppItgImage, int mm, int x, int y)
{
    int ss;
    ss=ppItgImage[x+mm][y+mm]+ppItgImage[x][y]-ppItgImage[x+mm][y]-ppItgImage[x][y+mm];
	return ss;
}

void CFindEye_HL::GetItgImage(int **pItgImage,BYTE **pImage, int ImageWidth, int ImageHeight)
{
   	int x,y;
	for (x=0; x<ImageWidth+1; x++) 
		for (y=0; y<ImageHeight+1; y++) 
			pItgImage[x][y]=0;
	
	int* s = new int[ImageHeight];

	for (x=0; x<ImageWidth; x++)
		for ( y=0; y<ImageHeight; y++)
		{
			if (y!=0) 
				s[y]=s[y-1]+pImage[x][y];    
			else  
				s[y]=pImage[x][y];

			pItgImage[x+1][y+1]=pItgImage[x][y+1]+s[y];		
		}
/*	CFile curFile;
	curFile.Open("C:\\test.DB", CFile::modeCreate|CFile::modeWrite);
	

		for( int i = 0; i < 40+1; i++)
			curFile.Write(pItgImage[i], sizeof(int)*(40+1));
	
	curFile.Close();
    SaveBmpImg("C:\\test.bmp",pImage,40,40);*/
    delete []s;
//	SAFE_RELEASE(s);
}

